package com.summer.front;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.summer.common.Const;
import com.summer.common.ResponseCode;
import com.summer.common.ServerResponse;
import com.summer.user.pojo.User;
import com.summer.user.pojo.UserLogin;
import com.summer.user.service.IUserService;

/** 
 * @描述：网站前台的用户（User）相关接口
 * @作者：yang 
 * @时间：2017年6月20日 下午9:59:43 
 * @版本：1.0
 */
@RestController
@RequestMapping("/user/")
public class UserController {

	@Resource
	private IUserService iUserService;
	
	/**
	 * 1、用户登录接口
	 * @param username
	 * @param password
	 * @param session
	 * @return
	 */
	@PostMapping("login")
	public ServerResponse<UserLogin> login(String username, String password, HttpSession session) {
		ServerResponse<UserLogin> response = iUserService.login(username, password);
		if (response.isSuccess()) {
			session.setAttribute(Const.CURRENT_USER, response.getData());
		}
		return response;
	}
	
	/**
	 * 2、退出登录接口
	 * @param session
	 * @return
	 */
	@PostMapping("logout")
	public ServerResponse<String> logout(HttpSession session) {
		session.removeAttribute(Const.CURRENT_USER);
		return ServerResponse.createBySuccess();
	}
	
	/**
	 * 3、用户注册
	 * @param user
	 * @return
	 */
	@PostMapping("register")
	public ServerResponse<String> register(User user) {
		return iUserService.register(user);
	}
	
	/**
	 * 4、实时校验 用户名 或者 邮箱是否已经注册（根据type的值是email还是username，来判断）
	 * @param str
	 * @param type
	 * @return
	 */
	@PostMapping("checkvalid")
	public ServerResponse<String> checkValid(String str, String type) {
		return iUserService.checkValid(str, type);
	}
	
	/**
	 * 5、获取Session中用户的信息接口
	 * @param session
	 * @return
	 */
	@PostMapping("user_info_session")
	public ServerResponse<UserLogin> getUserInfoSession(HttpSession session) {
		UserLogin user = (UserLogin) session.getAttribute(Const.CURRENT_USER);
		if (user != null) {
			return ServerResponse.createBySuccess(user);
		}
		return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
	}
	
	/**
	 * 6、获取用户详细信息
	 * @param session
	 * @return
	 */
	@PostMapping("user_info")
	public ServerResponse<User> getInfo(HttpSession session) {
		UserLogin currentUser = (UserLogin)session.getAttribute(Const.CURRENT_USER);
		if(currentUser == null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "需登录后才可获取用户信息！");
		}
		return iUserService.getInformation(currentUser.getId());
	}
	
	/**
	 * 7、忘记密码情况下，发送邮件，重置密码
	 * @param email
	 * @return
	 */
	@PostMapping("reset_pwd_email")
	public ServerResponse<String> sendEmailForForget(String email) {
		return iUserService.sendEmailForForget(email);
	}
	
	/**
	 * 8、忘记密码情况下，重置密码操作
	 * @param email
	 * @param passwordNew
	 * @param token
	 * @return
	 */
	@PostMapping("forget_reset_pwd")
	public ServerResponse<String> forgetResetPassword(String email, String passwordNew, String token) {
		return iUserService.forgetResetPassword(email, passwordNew, token);
	}
	
	/**
	 * 9、登录状态下，重置密码
	 * @param session
	 * @param passwordOld
	 * @param passwordNew
	 * @return
	 */
	@PostMapping("reset_pwd")
	public ServerResponse<String> resetPassword(HttpSession session, String passwordOld, String passwordNew) {
		UserLogin user = (UserLogin)session.getAttribute(Const.CURRENT_USER);
		if(user == null) {
			return ServerResponse.createByErrorMessage("用户未登录！");
		}
		
		return iUserService.resetPassword(passwordOld, passwordNew, user.getId());
	}
	
	/**
	 * 10、更新用户信息
	 * @param session
	 * @param user
	 * @return
	 */
	@PostMapping("update_info")
	public ServerResponse<User> updateInfo(HttpSession session, User user) {
		UserLogin currentUser = (UserLogin) session.getAttribute(Const.CURRENT_USER);
		if(currentUser == null) {
			return ServerResponse.createByErrorMessage("用户未登录");
		}
		user.setId(currentUser.getId());
		
		ServerResponse<User> response = iUserService.updateInformation(user);
		
		if(response.isSuccess()) {
			currentUser.setUsername(user.getUsername());
			session.setAttribute(Const.CURRENT_USER, currentUser);
		}
		return response;
	}
	
	
}
